package com.pedro.streamer.manager;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;


import com.pedro.streamer.model.AudioItem;
import com.pedro.streamer.service.FloatingMediaService;

import java.util.List;

public class MediaPlayerClient {

    public enum PlayMode {
        ListPlay,
        RandomPlay
    }

    private static VideoControlManager videoControlManager;
    private static final String TAG = "MediaPlayerClient";

    private static MediaPlayerClient mInstance = null;
    private Context mContext;
    private FloatingMediaService.FloatingBinder floatingBinder;

    private videoSyncListen videoSyncListen;

    public MediaPlayerClient(Context context) {
        mContext = context;
    }

    public static MediaPlayerClient getInstance(Context context) {
        synchronized (MediaPlayerClient.class) {
            if (mInstance == null) {
                mInstance = new MediaPlayerClient(context);
                videoControlManager = VideoControlManager.getInstance();
            }
        }
        return mInstance;
    }



    private PlayMode mPlayMode = PlayMode.RandomPlay;
    private long mDelayTime = 10000; //默认10s
    private float supVolume = 0.5f;
    private float bgmVolume = 0.5f;

    public void bindService(Intent intent) {
        mContext.bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.d(TAG, "浮窗服务绑定成功");
                floatingBinder = (FloatingMediaService.FloatingBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        }, Context.BIND_AUTO_CREATE);
    }


    public void playRandomSong(boolean isOpen) {
        if (floatingBinder != null) {
            floatingBinder.playRandomMusic(isOpen);
//            floatingBinder.updateLetMusicPlay(isOpen);
        }
    }

    public boolean isSupPlaying() {
        if (floatingBinder != null){
            return floatingBinder.isLetMusicPlay();
        }
        return false;
    }

    public boolean isBgmPlaying() {
        return floatingBinder.isLetBgmPlay();
    }

    public void playPre() {
        if (floatingBinder != null) {
            floatingBinder.playPre();
        }
    }

    public void playNext() {
        if (floatingBinder != null) {
            floatingBinder.playNext();
        }
    }

    public void playBgm(boolean isOpen) {
        if (floatingBinder != null) {
            floatingBinder.playBgmMusic(isOpen);
        }
    }

    public void playBgmAtPosition(int position) {
        if (floatingBinder != null) {
            floatingBinder.playBgmAtPosition(position);
        }
    }

    public void updateMusicVolume(float volume) {
        supVolume = volume;
        if (floatingBinder != null) {
            floatingBinder.updateMusicVolume(volume, volume);
        }
    }

    public float getSupVolume() {
        return supVolume;
    }

    public void updateBgmVolume(float volume) {
        bgmVolume = volume;
        if (floatingBinder != null) {
            floatingBinder.updateBgmVolume(volume, volume);
        }

    }

    public float getBgmVolume() {
        return bgmVolume;
    }

    public void updatePlayList(List<AudioItem> audioItems) {
        if (floatingBinder != null) {
            floatingBinder.updatePlayList(audioItems);
        }
    }

    public void updateBgmPlayList(List<AudioItem> audioItems) {
        if (floatingBinder != null) {
            floatingBinder.updateBgmPlayList(audioItems);
        }
    }

    public void setPlayMode(PlayMode playMode) {
        mPlayMode = playMode;
        if (floatingBinder != null) {
            floatingBinder.updateRandomState(playMode);
        }
    }

    public PlayMode getPlayMode() {
        return mPlayMode;
    }

    public void setDelayTime(long delayTime) {
        mDelayTime = delayTime;
        if (floatingBinder != null) {
            floatingBinder.updateDelayTime(delayTime);
        }
    }

    public long getDelayTime() {
        return mDelayTime;
    }

    public List<AudioItem> getBmgList() {
        return floatingBinder.getBgmList();
    }

    public List<AudioItem> getMusicList() {
        return floatingBinder.getMusicList();
    }


    //audio control
    public void showControlView(){
        if (floatingBinder != null) {
            floatingBinder.showControl();
        }
    }
    public void showFloatView(boolean bool){
        if (floatingBinder != null) {
            floatingBinder.showFloatView(bool);
        }
    }

    public void showAudioView(){
        if (floatingBinder != null) {
            floatingBinder.showAudioView();
        }
    }



    public void showVideoView(){
        if (floatingBinder != null) {
            floatingBinder.showVideoView();
        }
    }

    public void showClipView(){
        if (floatingBinder != null) {
            floatingBinder.showClipView();
        }
    }

    public void setVideoSyncListen(MediaPlayerClient.videoSyncListen videoSyncListen) {
        this.videoSyncListen = videoSyncListen;
    }

    public MediaPlayerClient.videoSyncListen getVideoSyncListen() {
        return videoSyncListen;
    }

    public interface  videoSyncListen{
        public void videoSync();
    }

}
